// mock-ws-server.js
const WebSocket = require('ws');

const wss = new WebSocket.Server({ port: 8080 });

function randomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function randomName() {
  const names = ['小红', '小明', '小白', '小黑', '小黄', '小绿', '小蓝', '小紫', '小青', '小橙'];
  return names[randomInt(0, names.length - 1)];
}

function randomMajor() {
  const majors = ['大数据', '机器人', '数学', '物理', '化学', '生物', '英语', '体育', '音乐', '美术'];
  return majors[randomInt(0, majors.length - 1)];
}

function randomDorm() {
  return randomInt(100, 299).toString();
}

function genTableData() {
  return Array.from({ length: 6 }, () => [
    randomName(),
    randomInt(1, 12) + '号楼',
    `2024-06-01 ${randomInt(6, 23)}:${randomInt(0,59).toString().padStart(2,'0')}`,
    Math.random() > 0.5 ? '进门' : '出门'
  ]);
}

function genRankingData() {
  const colleges = [
    '人工智能学院', '教育教育学院', '经济管理学院', '信息工程学院',
    '文学院', '文博学院', '艺术设计学院'
  ];
  return colleges.map(name => ({
    name,
    value: randomInt(20, 99)
  }));
}

function genDormList() {
  return Array.from({ length: 10 }, () => [
    randomDorm(),
    randomName(),
    randomMajor()
  ]);
}

function sendAll(type, data) {
  wss.clients.forEach(client => {
    if (client.readyState === WebSocket.OPEN) {
      client.send(JSON.stringify({ type, data }));
    }
  });
}

wss.on('connection', ws => {
  console.log('前端已连接 WebSocket');

  // 首次连接立即推送一次
  sendAll('card1', {
    name: randomName(),
    dorm: randomDorm(),
    major: randomMajor(),
    photo: ''
  });
  sendAll('card2', {
    name: randomName(),
    dorm: randomDorm(),
    major: randomMajor(),
    photo: ''
  });
  sendAll('number', {
    out: randomInt(100, 200),
    in: randomInt(50, 100),
    notBack: randomInt(5, 20),
    notOut: randomInt(1, 10)
  });
  sendAll('pie', {
    bed: [randomInt(800, 1200), randomInt(100, 400)],
    inout: [
      randomInt(100, 200), // 出舍人数
      randomInt(50, 100),  // 入舍人数
      randomInt(5, 20),    // 长期未出人数
      randomInt(1, 10)     // 长期未归人数
    ]
  });
  sendAll('table', genTableData());
  sendAll('excellent', genDormList());
  sendAll('bad', genDormList());
  sendAll('ranking', genRankingData());
  sendAll('notice', '请所有宿舍成员注意，今晚7点将进行消防演练，请准时参加！保持宿舍卫生，营造良好环境。');

  // 定时推送数据
  const interval = setInterval(() => {
    sendAll('card1', {
      name: randomName(),
      dorm: randomDorm(),
      major: randomMajor(),
      photo: ''
    });
    sendAll('card2', {
      name: randomName(),
      dorm: randomDorm(),
      major: randomMajor(),
      photo: ''
    });
    sendAll('number', {
      out: randomInt(100, 200),
      in: randomInt(50, 100),
      notBack: randomInt(5, 20),
      notOut: randomInt(1, 10)
    });
    sendAll('pie', {
      bed: [randomInt(800, 1200), randomInt(100, 400)],
      inout: [
        randomInt(100, 200),
        randomInt(50, 100),
        randomInt(5, 20),
        randomInt(1, 10)
      ]
    });
    sendAll('table', genTableData());
    sendAll('excellent', genDormList());
    sendAll('bad', genDormList());
    sendAll('ranking', genRankingData());
    sendAll('notice', '请所有宿舍成员注意，今晚7点将进行消防演练，请准时参加！保持宿舍卫生，营造良好环境。');
  }, 5000);

  ws.on('close', () => {
    clearInterval(interval);
    console.log('前端断开 WebSocket');
  });
});

console.log('本地WebSocket服务已启动，端口: 8080');